package com.uwlaser.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.uwlaser.base.PageInfo;
import com.uwlaser.base.QueryHelpMybatisPlus;
import com.uwlaser.base.impl.CommonServiceImpl;
import com.uwlaser.domain.FactoryShift;
import com.uwlaser.domain.FactoryShiftTime;
import com.uwlaser.service.FactoryShiftHistoryService;
import com.uwlaser.service.FactoryShiftTimeService;
import com.uwlaser.service.dto.FactoryShiftTimeDto;
import com.uwlaser.service.dto.FactoryShiftTimeQueryParam;
import com.uwlaser.service.mapper.FactoryShiftMapper;
import com.uwlaser.service.mapper.FactoryShiftTimeMapper;
import com.uwlaser.utils.ConvertUtil;
import com.uwlaser.utils.PageUtil;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author huangpan
 * @date 2022-12-02
 */
@Service
@AllArgsConstructor
// @CacheConfig(cacheNames = FactoryShiftTimeService.CACHE_KEY)
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class FactoryShiftTimeServiceImpl extends CommonServiceImpl<FactoryShiftTimeMapper, FactoryShiftTime> implements FactoryShiftTimeService {

    // private final RedisUtils redisUtils;
    private final FactoryShiftTimeMapper factoryShiftTimeMapper;

    private final FactoryShiftMapper factoryShiftMapper;

    private final FactoryShiftHistoryService factoryShiftHistoryService;

    @Override
    public PageInfo<FactoryShiftTimeDto> queryAll(FactoryShiftTimeQueryParam query, Pageable pageable) {
        IPage<FactoryShiftTime> queryPage = PageUtil.toMybatisPage(pageable);
        IPage<FactoryShiftTime> page = factoryShiftTimeMapper.selectPage(queryPage, QueryHelpMybatisPlus.getPredicate(query));
        return ConvertUtil.convertPage(page, FactoryShiftTimeDto.class);
    }

    @Override
    public List<FactoryShiftTimeDto> queryAll(FactoryShiftTimeQueryParam query) {
        return ConvertUtil.convertList(factoryShiftTimeMapper.selectList(QueryHelpMybatisPlus.getPredicate(query)), FactoryShiftTimeDto.class);
    }

    @Override
    public FactoryShiftTime getById(Integer id) {
        return factoryShiftTimeMapper.selectById(id);
    }

    @Override
    // @Cacheable(key = "'id:' + #p0")
    public FactoryShiftTimeDto findById(Integer id) {
        return ConvertUtil.convert(getById(id), FactoryShiftTimeDto.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insert(FactoryShiftTimeDto resources) {
        FactoryShiftTime entity = ConvertUtil.convert(resources, FactoryShiftTime.class);
        return factoryShiftTimeMapper.insert(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateById(FactoryShiftTimeDto resources) {
        FactoryShiftTime entity = ConvertUtil.convert(resources, FactoryShiftTime.class);
        //获取班次模板
        FactoryShift factoryShift = factoryShiftMapper.selectById(resources.getShiftId());
        //添加班次历史数据
        factoryShiftHistoryService.add(factoryShift.getTemplateCode());
        int ret = factoryShiftTimeMapper.updateById(entity);
        // delCaches(resources.id);
        return ret;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int removeByIds(Set<Integer> ids) {
        // delCaches(ids);
        //获取班次休息时间信息
        FactoryShiftTime factoryShiftTime = factoryShiftTimeMapper.selectById(ids.stream().findFirst().get()); //获取班次模板
        FactoryShift factoryShift = factoryShiftMapper.selectById(factoryShiftTime.getShiftId());
        //添加班次历史数据
        factoryShiftHistoryService.add(factoryShift.getTemplateCode());
        return factoryShiftTimeMapper.deleteBatchIds(ids);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int removeById(Integer id) {
        Set<Integer> set = new HashSet<>(1);
        set.add(id);
        return this.removeByIds(set);
    }

    /*
    private void delCaches(Integer id) {
        redisUtils.delByKey(CACHE_KEY + "::id:", id);
    }

    private void delCaches(Set<Integer> ids) {
        for (Integer id: ids) {
            delCaches(id);
        }
    }*/

    /*
    @Override
    public void download(List<FactoryShiftTimeDto> all, HttpServletResponse response) throws IOException {
      List<Map<String, Object>> list = new ArrayList<>();
      for (FactoryShiftTimeDto factoryShiftTime : all) {
        Map<String,Object> map = new LinkedHashMap<>();
              map.put("班次ID", factoryShiftTime.getShiftId());
              map.put("休息开始时间", factoryShiftTime.getStartTime());
              map.put("休息结束时间", factoryShiftTime.getEndTime());
              map.put("备注说明", factoryShiftTime.getRemark());
              map.put("创建人", factoryShiftTime.getCreateBy());
              map.put("创建时间", factoryShiftTime.getCreateTime());
              map.put("更新人", factoryShiftTime.getUpdateBy());
              map.put("更新时间", factoryShiftTime.getUpdateTime());
              map.put("类型;(1.开会,2.吃饭,3.其他)", factoryShiftTime.getType());
              map.put("是否禁用;0：否，1：是", factoryShiftTime.getIsDisable());
        list.add(map);
      }
      FileUtil.downloadExcel(list, response);
    }*/
}
